/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * MyspaceServlet.java
 *
 * 网页的响应Servlet
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sundy.core.Common;
import com.sundy.core.Configuration;
import com.sundy.core.Context;
import com.sundy.core.Utility;
import com.sundy.daosupport.DescriptTblDAO;
import com.sundy.daosupport.RightTblDAO;
import com.sundy.daosupport.SubjectTblDAO;
import com.sundy.daosupport.TeacherTblDAO;
import com.sundy.service.DescriptTbl;
import com.sundy.service.RightTbl;
import com.sundy.service.SubjectTbl;
import com.sundy.service.TeacherTbl;

public class MyspaceServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1392861456761083484L;

	public MyspaceServlet() {

	}

	protected void doGet(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(context, request, response);
	}

	protected void doPost(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RightTblDAO dao = RightTblDAO
				.getFromApplicationContext(Common.applicationContext);
		List<RightTbl> list = dao.findByTeacherId(context.getCurrentUser()
				.getId());
		DescriptTblDAO descdao = DescriptTblDAO
				.getFromApplicationContext(Common.applicationContext);
		List<DescriptTbl> desctbl = descdao.findAll();
		String exportDirPath = Configuration.getProperty("/config/export-dir");
		File exportDir = new File(exportDirPath);
		List<String> exports = new ArrayList<String>();
		File[] files = exportDir.listFiles();
		if (files != null) {
			for (File file : files) {
				if (file.getName().endsWith(".xls"))
					exports.add(file.getName());
			}
		}
		if (context.getCurrentUser().getAdmin() == 1) {
			TeacherTblDAO usrdao = TeacherTblDAO
					.getFromApplicationContext(Common.applicationContext);
			List<TeacherTbl> usrlist = usrdao.findAll();
			SubjectTblDAO subdao = SubjectTblDAO.getFromApplicationContext(Common.applicationContext);
			List<SubjectTbl> sublist = subdao.findAll();
			request.setAttribute("lst", usrlist);
			request.setAttribute("lst2", desctbl);
			request.setAttribute("xls", exports);
			request.setAttribute("sublist", sublist);
			Utility.showJSP(request, response, "/manager.jsp");
		} else {
			if (list == null || list.isEmpty()) {
				Utility.showInfor(request, response, "您没有被授权，请与管理员联系！");
				return;
			} else {
				request.setAttribute("lst2", desctbl);
				Utility.showJSP(request, response, "/normal.jsp");
			}
		}
	}
}
